package com.lagou.listener;

import com.lagou.entity.Order;
import com.lagou.entity.OrderDetail;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 监听订单队列，用户生成订单，存放到消息队列MQ，触发写数据库操作
 */
@Component
public class MyOrderListener {

    @RabbitListener(queues = "q.order", ackMode = "MANUAL")
    public void onMessage(Order order, Channel channel, Message message) throws IOException {
        System.out.println("写数据库");
        System.out.println("订单id:["+order.getOrderId()
                + "];用户id:[" + order.getUserId()
                + "];订单状态:[" + order.getStatus()+"]");
        for (OrderDetail detail : order.getDetails()) {
            System.out.println(
                    "商品id:["+detail.getItemId() + "];商品名称:["
                    + detail.getItemName() + "];商品价格:["
                    + detail.getItemPrice() + "];数量:["
                    + detail.getNum()+"]"
            );
        }
        //手动确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
